package com.ogawa.base.utils;

import android.text.TextUtils;

import java.nio.ByteBuffer;
import java.util.UUID;

public class HexUtil {

    /**
     * 2 进制转 16 进制
     *
     * @param bytes
     * @return
     */
    public static String byte2Hex(byte[] bytes) {
        String string = "";
        for (byte b : bytes) {
            String hex = Integer.toHexString(b & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            string = string + hex.toUpperCase();
        }
        return string;
    }



    /**
     * 16 进制转 10 进制（数字）
     *
     * @param str
     * @return
     */
    public static int hexToInt(String str) {
        if(TextUtils.isEmpty(str)){
            return 0;
        }
        int heightBit = Integer.parseInt(str.substring(0, 1), 16);
        int lowBit = Integer.parseInt(str.substring(1, 2), 16);
        return heightBit * 16 + lowBit;
    }

    /**
     * 16 进制转 10 进制（字符串）
     *
     * @param str
     * @return
     */
    public static String hexToIntString(String str) {
        return String.valueOf(hexToInt(str));
    }

    /**
     * 返回 0 的个数
     *
     * @param i
     * @return
     */
    private static String toReturn0(int i) {
        String str = "";
        for (int j = 0; j < i; j++) {
            str = str + "0";
        }
        return str;
    }

    /**
     * 16 进制转 2 进制
     *
     * @param hexString
     * @return 结果以 byte 数组来存储
     * 注意：| 的结果超过 127（1111111，7个1，只要超过7位就代表超过 127） 时，
     * 在计算机中时用补码的形式来表示的，补码的结果为原数取反+1
     */
    public static byte[] hexToByte(String hexString) {
        if (hexString == null || hexString.equals("")) {
            return null;
        }
        hexString = hexString.toUpperCase();
        int length = hexString.length() / 2;
        char[] hexChars = hexString.toCharArray();
        byte[] d = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
        }
        return d;
    }

    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }

    /**
     * uuid转化 byte[]
     *
     * @param uuid
     * @return
     */
    public static byte[] toByte(UUID uuid) {
        ByteBuffer buffer = ByteBuffer.wrap(new byte[16]);
        buffer.putLong(uuid.getMostSignificantBits());
        buffer.putLong(uuid.getLeastSignificantBits());
        return buffer.array();
    }

    /**
     * byte[] 转换 uuid
     *
     * @param bytes
     * @return
     */
    public static UUID toUUID(byte[] bytes) {
        ByteBuffer buffer = ByteBuffer.wrap(bytes);
        long fistLong = buffer.getLong();
        long secondLong = buffer.getLong();
        return new UUID(fistLong, secondLong);
    }

    /**
     * 16进制累加算法
     *
     * @param hexStr
     * @return
     */
    public static String getHexSum(String hexStr) {
        if (null == hexStr || "".equals(hexStr)) {
            return "00";
        }
        hexStr = hexStr.replaceAll(" ", "");
        int length = hexStr.length();
        if (0 != length % 2) {
            return "00";
        }
        int total = 0;
        int num = 0;
        while (num < length) {
            String s = hexStr.substring(num, num + 2);
            total += Integer.parseInt(s, 16);
            num += 2;
        }
        // 用 256 求余最大是255，即 16 进制的 FF
        String hex = Integer.toHexString(total % 256);
        length = hex.length();
        return length < 2 ? "0" : "" + hex;
    }


    /**
     * 10 进制转 16 进制
     *
     * @param
     * @return
     */
        public static String decimalToHex(int decimal) {
            String hex = "";
            while(decimal != 0) {
                int hexValue = decimal % 16;
                hex = toHexChar(hexValue) + hex;
                decimal = decimal / 16;
            }
            return  hex;
        }
        //将0~15的十进制数转换成0~F的十六进制数
        public static char toHexChar(int hexValue) {
            if(hexValue <= 9 && hexValue >= 0)
                return (char)(hexValue + '0');
            else
                return (char)(hexValue - 10 + 'A');
        }




}
